define(['moment'], function (moment) {
    return ['$scope', '$rootScope', '$mdDialog', 'MMWService', '$filter', '$stateParams', 'config',
        function ($scope, $rootScope, $mdDialog, $MMWService, $filter, $stateParams, config) {
            $scope.select_machines = {};
            $scope.init = function(){
                $scope.filter = {type:{},machine:{}};
                $scope.select_type={};
                if($stateParams.data.select_machines && $stateParams.data.select_machines.length>0){
                    $stateParams.data.select_machines.forEach(function(machine){
                        $scope.select_machines[machine.machine_no] = true;
                    });
                }
                getMachineType(function(){
                    getMachine($scope.select_type, function(){

                    });
                });
            };

            $scope.back = function(){
                $rootScope.changeProgram($stateParams.data.parent, true);
            };

            $scope.changeType = function(type){
                $scope.select_type = type;
                getMachine($scope.select_type, function(){

                });
            };

            $scope.selectMachine = function(machine){
                if($scope.select_machines[machine.page_no]){
                    $scope.select_machines[machine.page_no] = false;
                } else {
                    $scope.select_machines[machine.page_no] = true;
                }
            };

            $scope.confirm = function(){
                var content = {
                    master_no:config.cache.account,
                    master_type : '0',
                    favourite_machine_detail : []
                };
                for(var index in $scope.select_machines){
                    if($scope.select_machines[index])
                        content.favourite_machine_detail.push({machine_no:index});
                }

                if(content.favourite_machine_detail.length>0){
                    $MMWService.sendToServer({
                        uri: 'ServicesSTD.Module_SYS.favourite_machine_process',
                        content : content,
                        success : function(data){
                            $rootScope.changeProgram($stateParams.data.parent, true);
                        }
                    });
                } else {
                    $scope.$root.showAlert($filter('translate')('mobile00.msg.plz_select_one'));
                }
            };

            function getMachineType(callback){
                $MMWService.pageNumberInfoGet({
                    page_mode : '11'
                }
                , function(itemList){
                    $scope.machineTypes = itemList;
                    if($scope.machineTypes.length>0){
                        $scope.select_type = $scope.machineTypes[0];
                        callback();
                    }
                });
            }

            function getMachine(typeItem, callback){
                $MMWService.pageNumberInfoGet({
                    page_mode : '2',
                    query_condition_info:[{column_name:'EquipmentType', query_mode:'0', value: typeItem.page_no, merge_condition_model:'AND'}]
                }, function(itemList){
                    $scope.machines = itemList;
                    callback();
                });
            }
        }
    ];
});
